package com.sxf.rental;

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.Collections;

@SpringBootTest
public class GeneratorCode {
    private static final String AUTHOR = "sxf";
    private static final String JDBC_URL = "jdbc:mysql://localhost:3306/auto_rental";
    private static final String JDBC_USERNAME = "root";
    private static final String JDBC_PASSWORD = "root";
    private static final String OUT_DIR = ".\\src\\main\\java";
    private static final String PACKAGE_NAME = "com.sxf";
    private static final String MODULE_NAME = "rental";
    private static final String[] TABLES = {"auto_maker", "auto_brand", "auto_info",
            "sys_dept", "sys_permission", "sys_role", "sys_role_permission", "sys_user_role", "sys_user",
            "busi_customer", "busi_maintain", "busi_violation", "busi_rental_type", "busi_order"
    };
    private static final String[] PREFIX = {"sys_", "busi_"};
   @Test
    void generateCode() {
        FastAutoGenerator.create(JDBC_URL, JDBC_USERNAME, JDBC_PASSWORD)
                .globalConfig(builder -> builder
                        .author(AUTHOR) // 设置作者
                        .enableSwagger() // 启用Swagger
                        .outputDir(OUT_DIR) // 设置输出目录
                )
                .packageConfig(builder -> builder
                        .parent(PACKAGE_NAME) // 设置父包名
                        .moduleName(MODULE_NAME) // 设置模块名
                        .pathInfo(Collections.singletonMap(OutputFile.xml,".\\src\\main\\resources\\mapper")) // 设置XML生成路径
                )
                .strategyConfig(builder -> builder
                        .addInclude(TABLES) // 设置需要生成的表
                        .addTablePrefix(PREFIX) // 设置表前缀
                        .entityBuilder()
                        .enableLombok() // 启用Lombok
                        .enableChainModel() // 启用链式模型
                        .controllerBuilder()
                        .enableRestStyle() // 启用Rest风格
                )
                .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板
                .execute();
    }
}
